/**
 * Copyright (c) 2009, http://www.softwareinlife.com. All rights reserved.
 * 
 * Redistribution and use in source and binary forms, 
 * with or without modification, are permitted provided that 
 * the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright notice, 
 *    this list of conditions and the following disclaimer.
 * 
 * 2. Redistributions in binary form must reproduce the above copyright notice, 
 *    this list of conditions and the following disclaimer in the documentation 
 *    and/or other materials provided with the distribution.
 * 
 * 3. Neither the name of the http://www.softwareinlife.com nor the names of its 
 *    contributors may be used to endorse or promote products derived from 
 *    this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE SOFTWAREINLIFE AND CONTRIBUTORS "AS IS" 
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 * ARE DISCLAIMED. 
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**/

/**
 * @author Kang, tae-ho (usteam@softwareinlife.com)
 * @date 2009. 10. 11.
 */



package com.softwareinlife.vision.android.ux.start;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import org.apache.http.HttpResponse;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.softwareinlife.vision.android.R;
import com.softwareinlife.vision.android.communication.SyncXmlParser;
import com.softwareinlife.vision.android.communication.model.VisionModel;
import com.softwareinlife.vision.android.communication.model.VisionSoft;
import com.softwareinlife.vision.android.communication.sync.SyncManager;
import com.softwareinlife.vision.android.data.dao.VisionDAO;
import com.softwareinlife.vision.android.data.exceptions.DatabaseConnectionException;
import com.softwareinlife.vision.android.data.exceptions.VisionCreateException;
import com.softwareinlife.vision.android.data.exceptions.VisionReadException;
import com.softwareinlife.vision.android.data.exceptions.VisionUpdateException;
import com.softwareinlife.vision.android.system.model.BaseModel;
import com.softwareinlife.vision.android.ux.about.AboutContactActivity;
import com.softwareinlife.vision.android.ux.about.AboutSoftwareinlifeActivity;
import com.softwareinlife.vision.android.ux.plan.ReadPlanActivity;
import com.softwareinlife.vision.android.ux.schedule.ReadScheduleActivity;
import com.softwareinlife.vision.android.ux.util.SharedPreferenceUtility;
import com.softwareinlife.vision.android.ux.vision.CreateVisionActivity;
import com.softwareinlife.vision.android.ux.vision.ReadVisionActivity;

public class VisionSoftwareMenuActivity extends Activity {
	
	// VISION BUTTON /////////////////////////////////////

	/**
	 * Button of vision
	 */
	private Button visionlistButton = null;
	
	//EDIT VISION BUTTON /////////////////////////////////////

	/**
	 * Button of edit vision
	 */
	private Button editvisionButton = null;
	
	// PLAN BUTTON /////////////////////////////////////

	/**
	 * Button of plan
	 */
	private Button planlistButton = null;
	
	// EDIT PLAN BUTTON /////////////////////////////////////

	/**
	 * Button of plan
	 */
	private Button editplanButton = null;
	
	// SCHEDULE BUTTON /////////////////////////////////////

	/**
	 * Button of schedule
	 */
	private Button schedulelistButton = null;
	
	// EDIT SCHEDULE BUTTON /////////////////////////////////////

	/**
	 * Button of editschedule
	 */
	private Button editscheduleButton = null;
	
	/**
	 * Button of Syncronize
	 */
	private Button syncronize = null;
	
	// ABOUT BUTTON /////////////////////////////////////

	/**
	 * Button of about
	 */
	private Button aboutButton = null;
	
	// CONTACT BUTTON /////////////////////////////////////

	/**
	 * Button of contact
	 */
	private Button contactButton = null;
	
		
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	
		setContentView(R.layout.start_vision_menu);
		
		// Initialize Members
		initMembers();
		
	}
	
	public void initMembers(){
		
		// VISION BUTTON //////////////////////////////////
		visionlistButton = (Button) findViewById(R.id.vision_list);
		visionlistButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent i = new Intent(VisionSoftwareMenuActivity.this, ReadVisionActivity.class);
				startActivity(i);
			}
		});
		
		//EDIT VISION BUTTON //////////////////////////////////
		editvisionButton = (Button) findViewById(R.id.vision_create);
		editvisionButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent i = new Intent(VisionSoftwareMenuActivity.this, CreateVisionActivity.class);
				startActivity(i);
			}
		});
		
		// PLAN BUTTON //////////////////////////////////
		planlistButton = (Button) findViewById(R.id.plan_list);
		planlistButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent i = new Intent(VisionSoftwareMenuActivity.this, ReadPlanActivity.class);
				startActivity(i);
			}
		});
		
//		//EDIT PLAN BUTTON //////////////////////////////////
//		editplanButton = (Button) findViewById(R.id.plan_edit);
//		editplanButton.setOnClickListener(new OnClickListener() {
//			public void onClick(View v) {
//				Intent i = new Intent(VisionSoftwareMenuActivity.this, CreatePlanActivity.class);
//				startActivity(i);
//			}
//		});
		
		//SCHEDULE BUTTON //////////////////////////////////
		schedulelistButton = (Button) findViewById(R.id.schedule_list);
		schedulelistButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent i = new Intent(VisionSoftwareMenuActivity.this, ReadScheduleActivity.class);
				startActivity(i);
			}
		});
		
//		//EDIT SCHEDULE BUTTON //////////////////////////////////
//		editscheduleButton = (Button) findViewById(R.id.schedule_edit);
//		editscheduleButton.setOnClickListener(new OnClickListener() {
//			public void onClick(View v) {
//				Intent i = new Intent(VisionSoftwareMenuActivity.this, CreateScheduleActivity.class);
//				startActivity(i);
//			}
//		});
		
		//SYNCRONIZE BUTTON //////////////////////////////////
		syncronize = (Button) findViewById(R.id.syncronize);
		
		final VisionSoftwareMenuActivity visionMenuActivity = this;
		
		syncronize.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				final ProgressDialog progDailog = ProgressDialog.show(
						visionMenuActivity, "syncManager", "Syncronizing ..", 
						true);
				
				new Thread() {
					public void run() {
						syncTester();
						progDailog.dismiss();
					}
				}.start();
				
				//syncTester();
			
			}
		});
		
		
		
		//ABOUT BUTTON //////////////////////////////////
		aboutButton = (Button) findViewById(R.id.about_list);
		aboutButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent i = new Intent(VisionSoftwareMenuActivity.this, AboutSoftwareinlifeActivity.class);
				startActivity(i);
			}
		});
		
		//CONTACT BUTTON //////////////////////////////////
		contactButton = (Button) findViewById(R.id.contact_list);
		contactButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent i = new Intent(VisionSoftwareMenuActivity.this, AboutContactActivity.class);
				startActivity(i);
			}
		});
	}
	
	/**
	 * sync瑜��뚯뒪�명븯湲��꾪븳 肄붾뱶 ..
	 * by CJY, 2009.10.27 (Tue)
	 */
	private void syncTester()
	{
		VisionSoft visionSoft = new VisionSoft();
		
		List<com.softwareinlife.vision.android.data.model.VisionModel> visionList = null;
		VisionDAO visionDao = null;
		
		try {
			visionDao = new VisionDAO(getApplicationContext());	
			visionList = visionDao.readSyncData();
		} catch (DatabaseConnectionException e) {
			e.printStackTrace();
		} catch (VisionReadException e) {
			e.printStackTrace();
		}
		
		for(com.softwareinlife.vision.android.data.model.VisionModel visionModel : visionList)
		{
			VisionModel visionModelForXml = new  VisionModel(visionModel);
			visionModelForXml.setAction(visionModel.CREATE);
			visionSoft.AddVisionModel(visionModelForXml);
		}
		
		SyncManager syncManager = new SyncManager();
		HttpResponse httpResponse = syncManager.doSync(visionSoft, 
				"testUser", "5");
		
//		HttpResponse httpResponse = syncManager.doSync(visionSoft, 
//				SharedPreferenceUtility.getUserId(getApplicationContext()), "5");
		
		try {
			visionSoft = SyncXmlParser.parse(
					httpResponse.getEntity().getContent());
			
			List<VisionModel> visionModels = visionSoft.getVisionModel();
			
			try
			{
				for(VisionModel visionModel : visionModels) 
				{
					String strAction = visionModel.getAction();
					// Not null
					visionModel.setUser(SharedPreferenceUtility.getUserModel(getApplicationContext()));
					
					Date now = new Date();
					visionModel.setCreatedDate(now);
					visionModel.setModifiedDate(now);
					visionModel.setSyncDate(now);
					
					if(true == strAction.equalsIgnoreCase(BaseModel.CREATE))
					{
						visionDao.create(visionModel);
					}
					else if(true == strAction.equalsIgnoreCase(BaseModel.UPDATE))
					{
						visionDao.updateSyncDate(visionModel.getClientId());
						visionDao.update(visionModel);
					}
					else if(true == strAction.equalsIgnoreCase(BaseModel.DELETE))
					{
						visionDao.delete(visionModel.getClientId());
						visionDao.delete(visionModel);
					}
				}	
			}
			catch(VisionCreateException e)
			{
				Log.e("VisionSoftwareMenuActivity.syncTest", e.getMessage());
			}
			catch(VisionUpdateException e)
			{
				Log.e("VisionSoftwareMenuActivity.syncTest", e.getMessage());	
			}
			
			
//			visionDao.updateSyncDate(ids);
			
			// �쒕쾭濡�遺�꽣 xml����諛쏅뒗吏��뺤씤�섍린 �꾪빐 �④꺼��..  
			// by CJY, 2009.10.27 (Tue) 
//			String generateXml = SyncXmlParser.generate(visionSoft);
//			if(false == generateXml.equalsIgnoreCase(""))
//				;
			
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			Log.e("VisionSoftwareMenuActivity.syncTest", e.getMessage());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			Log.e("VisionSoftwareMenuActivity.syncTest", e.getMessage());
		}
	}
}